package com.ruoyi.web.controller.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.construction.domain.ConstructionSite;
import com.ruoyi.construction.service.IConstructionSiteService;
import com.ruoyi.driver.domain.TDriverBindVehicle;
import com.ruoyi.driver.service.IDriverInfoService;
import com.ruoyi.driver.service.ITDriverBindVehicleService;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.modules.domain.SoilSiteInfo;
import com.ruoyi.modules.domain.TransportationCompany;
import com.ruoyi.modules.domain.VehicleInfo;
import com.ruoyi.modules.service.ISoilSiteInfoService;
import com.ruoyi.modules.service.ITransportationCompanyService;
import com.ruoyi.modules.service.IVehicleInfoService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wasterTran.domain.TConstructionWasteTransportationOrder;
import com.ruoyi.wasterTran.service.ITConstructionWasteTransportationOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @date 2024/12/25 20:52
 * @desc 司机端小程序相关接口
 **/
@RestController
@RequestMapping("/driver")
public class DriverController extends BaseController {

    @Autowired
    private SysLoginService loginService;


    @Autowired
    private IDriverInfoService driverInfoService;


    @Autowired
    private IConstructionSiteService constructionSiteService;


    @Autowired
    private ISoilSiteInfoService soilSiteInfoService;


    @Autowired
    private IVehicleInfoService vehicleInfoService;



    @Autowired
    private ITConstructionWasteTransportationOrderService tConstructionWasteTransportationOrderService;



    @Autowired
    private ITDriverBindVehicleService tDriverBindVehicleService;

    @Autowired
    private ISysUserService userService;


    @Autowired
    private ITransportationCompanyService transportationCompanyService;

    /**
     * 小程序登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult miniProgramLogin(@RequestBody LoginBody loginBody)
    {
        // 生成令牌
        return loginService.driverLogin(loginBody.getUsername(), loginBody.getPassword());
    }


    /**
     * 获取驾驶员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('driver:driverinfo:query')")
    @GetMapping("/getDriverInfo")
    public AjaxResult getDriverInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String driverId = loginUser.getDriverId();
        return success(driverInfoService.selectDriverInfoById(driverId));
    }

    /**
     * 分页查询项目工地信息列表
     */
    @PreAuthorize("@ss.hasPermi('construction:site:query')")
    @GetMapping("/constructionSitePage")
    public TableDataInfo constructionSitePage(ConstructionSite constructionSite)
    {
        startPage();
        List<ConstructionSite> list = constructionSiteService.selectConstructionSiteList(constructionSite);
        return getDataTable(list);
    }

    /**
     * 分页查询查询处置场所信息列表
     */
    @PreAuthorize("@ss.hasPermi('soil:site:page')")
    @GetMapping("/soilSiteInfoPage")
    public TableDataInfo soilSiteInfoPage(SoilSiteInfo soilSiteInfo)
    {
        startPage();
        List<SoilSiteInfo> list = soilSiteInfoService.selectSoilSiteInfoList(soilSiteInfo);
        return getDataTable(list);
    }


    /**
     * 查询车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('modules:info:list')")
    @GetMapping("/vehicleInfoPage")
    public TableDataInfo vehicleInfoPage(VehicleInfo vehicleInfo)
    {
        LoginUser loginUser = getLoginUser();
//        vehicleInfo.setTransportationCompanyId(loginUser.getTransportationCompanyId());
        TransportationCompany company = transportationCompanyService.selectTransportationCompanyById(loginUser.getTransportationCompanyId());
        if(null !=company){
            vehicleInfo.setTeamId(company.getOldTeamId());
        }
        startPage();
        List<VehicleInfo> list = vehicleInfoService.selectVehicleInfoList(vehicleInfo);
        return getDataTable(list);
    }

    /**
     * 查询建筑垃圾联运订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('wasterTran:order:list')")
    @GetMapping("/wasteTransportationOrderPage")
    public TableDataInfo wasteTransportationOrderPage(TConstructionWasteTransportationOrder tConstructionWasteTransportationOrder)
    {
        SysUser user = getLoginUser().getUser();
        tConstructionWasteTransportationOrder.setDriverUserId(user.getUserId());
        startPage();
        List<TConstructionWasteTransportationOrder> list = tConstructionWasteTransportationOrderService.selectTConstructionWasteTransportationOrderList(tConstructionWasteTransportationOrder);
        return getDataTable(list);
    }

    /**
     * 首页司机统计
     */
    @PreAuthorize("@ss.hasPermi('wasterTran:order:count')")
    @GetMapping("/countData")
    public AjaxResult countData()
    {
        SysUser user = getLoginUser().getUser();
        Long userId = user.getUserId();
        Map<String,Object> map =tConstructionWasteTransportationOrderService.countData(userId);
        return AjaxResult.success(map);
    }

    /**
     * 上车
     */
    @PreAuthorize("@ss.hasPermi('wasterTran:order:upper')")
    @PostMapping("/upperCar")
    public AjaxResult upperCar(@RequestBody TDriverBindVehicle driverBindVehicle)
    {
        SysUser user = getLoginUser().getUser();
        // 司机id
        Long userId = user.getUserId();
        driverBindVehicle.setDriverUserId(userId+"");
        TDriverBindVehicle bindVehicle = new TDriverBindVehicle();
        bindVehicle.setDriverUserId(userId+"");
        /** 上下车状态:0.上车 1.下车  */
        bindVehicle.setOnOffStatus("0");
//        bindVehicle.setCarCode(driverBindVehicle.getCarCode());
        List<TDriverBindVehicle> list = tDriverBindVehicleService.selectTDriverBindVehicleList(bindVehicle);
        if(list.size()>0){
            return AjaxResult.error("司机已经绑定车辆，请勿重复绑定");
        }
        return AjaxResult.success(tDriverBindVehicleService.upperCar(driverBindVehicle));
    }


    /**
     * 下车
     */
    @PreAuthorize("@ss.hasPermi('wasterTran:order:lower')")
    @PostMapping("/lowerrCar")
    public AjaxResult lowerCar(@RequestBody TDriverBindVehicle driverBindVehicle)
    {
        SysUser user = getLoginUser().getUser();
        // 司机id
        Long userId = user.getUserId();
        driverBindVehicle.setDriverUserId(userId+"");
        return AjaxResult.success(tDriverBindVehicleService.lowerCar(driverBindVehicle));
    }

    /**
     * 修改密码
     */
    @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
    @PostMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SysUser user)
    {
        SysUser sysUser = getLoginUser().getUser();
        // 司机id
        Long userId = sysUser.getUserId();
        user.setUserId(userId);
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getUserId());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(getUsername());
        return toAjax(userService.resetPwd(user));
    }

    /**
     * 修改电子联单状态
     */
    @PreAuthorize("@ss.hasPermi('transportat:order:edit')")
    @PostMapping("/changeOrder")
    public AjaxResult changeStatus(@RequestBody TConstructionWasteTransportationOrder tConstructionWasteTransportationOrder)
    {
        SysUser sysUser = getLoginUser().getUser();
        // 司机id
        Long userId = sysUser.getUserId();
        tConstructionWasteTransportationOrder.setDriverUserId(userId);
        tConstructionWasteTransportationOrder.setCreateBy(sysUser.getNickName());
        return toAjax(tConstructionWasteTransportationOrderService.changeStatus(tConstructionWasteTransportationOrder));
    }

    /**
     * 创建电子联单
     */
    @PreAuthorize("@ss.hasPermi('transportat:order:add')")
    @PostMapping("/addOrder")
    public AjaxResult addOrder(@RequestBody TConstructionWasteTransportationOrder tConstructionWasteTransportationOrder)
    {
        SysUser sysUser = getLoginUser().getUser();
        // 司机id
        Long userId = sysUser.getUserId();
        tConstructionWasteTransportationOrder.setDriverUserId(userId);
        tConstructionWasteTransportationOrder.setCreateBy(sysUser.getNickName());
        return success(tConstructionWasteTransportationOrderService.addOrder(tConstructionWasteTransportationOrder));
    }

    /**
     * 电子联单-->详情
     */
    @PreAuthorize("@ss.hasPermi('transportat:order:details')")
    @GetMapping("/details/{id}")
    public AjaxResult details(@PathVariable("id") String id)
    {
        return AjaxResult.success(tConstructionWasteTransportationOrderService.selectTConstructionWasteTransportationOrderById(id));
    }

    /**
     * 车辆记录-->施工工地
     * @param plateCode 车牌号
     * @param type 类型  0.施工工地 1.处置场所
     */
    @PreAuthorize("@ss.hasPermi('vehicle:records:query')")
    @GetMapping("/vehicleRecords")
    public AjaxResult vehicleRecords(@RequestParam("plateCode") String plateCode,
                                                     @RequestParam("type") String type)
    {
        SysUser sysUser = getLoginUser().getUser();
        // 司机id
        Long userId = sysUser.getUserId();
        return AjaxResult.success(tConstructionWasteTransportationOrderService.vehicleRecords(userId,plateCode,type));
    }

    /**
     * 司机上车绑定记录
     */
    @PreAuthorize("@ss.hasPermi('vehicle:records:query')")
    @GetMapping("/inCarDetails")
    public AjaxResult inCarDetails()
    {
        SysUser sysUser = getLoginUser().getUser();
        // 司机id
        Long userId = sysUser.getUserId();
        return success(vehicleInfoService.inCarDetails(userId));
    }


    /**
     * 车辆记录--->施工工地--->施工工地联单详情
     * 车辆记录--->处置场所--->处置场所联单详情
     * @param id 联单id
     */
    @PreAuthorize("@ss.hasPermi('transportat:records:query')")
    @GetMapping("/transportatDetails/{id}")
    public AjaxResult transportatDetails(@RequestPart("id") String id)
    {

        return success(tConstructionWasteTransportationOrderService.selectTConstructionWasteTransportationOrderById(id));
    }



}
